package com.modifiertech.lineage.commands;

import com.modifiertech.lineage.BaseConst;
import com.modifiertech.lineage.api.PlayerData;
import com.modifiertech.lineage.event.PlayerDataHandler;
import com.modifiertech.lineage.event.PlayerLevelUpEvent;
import com.modifiertech.lineage.handler.PlayerLevelUpHandler;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraftforge.common.MinecraftForge;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

public class CmdPlayerLevelModify extends CommandBase {

    @Override
    public String getName() {
        return "lineageplayerlv";
    }

    @Override
    public String getUsage(ICommandSender iCommandSender) {
        return "lineage.cmdplayerlevelmodify.useage";
    }

    @Override
    public void execute(MinecraftServer minecraftServer, ICommandSender iCommandSender, String[] strings) throws CommandException {
        //                          0    1       2
        //lineageplayerlv  @p add  10
        if (strings.length == 0) {
            throw new CommandException("cmdplayerlevelmodify.invalid.missing.playername");
        }
        if (strings.length == 1) {
            throw new CommandException("cmdplayerlevelmodify.invalid.missing.opttype");
        }
        //操作值
        int nums=0;
        try{
            nums=Integer.parseInt(strings[2]);
            if(nums<0)throw new Exception("-");
        }catch (Exception e){
            throw new CommandException("cmdplayerlevelmodify.invalid.optvalue.values");
        }
        boolean ifSilent=CmdHelper.ifExecAsSilent(strings);
        if(ifSilent)strings=CmdHelper.cutSilentCmd(strings);
        //get player
        EntityPlayerMP entityPlayerMP=strings.length>1 ? CommandBase.getPlayer(minecraftServer,iCommandSender,strings[0])
                :CommandBase.getCommandSenderAsPlayer(iCommandSender);
        PlayerData data = PlayerDataHandler.get(entityPlayerMP);
        long oldLevel=data.getLevel();
        long newLevel=data.getLevel()+nums;

        PlayerLevelUpHandler.INSTANCE
                .levelUp(entityPlayerMP,oldLevel,newLevel,0,data);
//        if(data.playerLevelUp(nums)){
//            data.saveAndSync();
//            CmdHelper.execServerSay(new TextComponentTranslation("lineage.levelup.msg",entityPlayerMP.getName(),oldlevel,newLevel));
//        }
        ;
    }
    @Nonnull
    @Override
    public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos targetPos) {
        if (args.length == 1) {
            return Arrays.asList(server.getPlayerList().getOnlinePlayerNames());
        }
        if (args.length == 2) {
            String optname = args[1];
            return BaseConst.LevelOperate.list.stream().filter(name -> name.getName().startsWith(optname))
                    .map(BaseConst.LevelOperate::getName)
                    .collect(Collectors.toList());        }
        return new ArrayList<>();
    }
}
